﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.DBUtility;

/// <summary>
///市场部统计
/// </summary>
public partial class qy_54tss_Admin_Sale_groupordersum : System.Web.UI.Page
{
    /// <summary>
    /// 分站条件
    /// </summary>
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    /// <summary>
    /// 订单条件
    /// </summary>
    private string orderWhere
    {
        get
        {
            object o = ViewState["orderWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["orderWhere"] = value;
        }
    }

    /// <summary>
    /// 排序字段
    /// </summary>
    protected string ordersort
    {
        get
        {
            object o = ViewState["ordersort"];
            return (o == null) ? "ordercount" : Convert.ToString(o);
        }
        set
        {
            ViewState["ordersort"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlWhere = " 1=1 ";

            orderWhere = "  1=1 and Custorder.delflag = 0  ";
            orderWhere = new RightsTool().getOrderWhere(orderWhere);

            WebUtility.SetDDLCity(ddlcity);

            new OrderState().BindOrderState(rblstate);

            InitSqlwhere();
        }
    }

    /// <summary>
    /// 绑定指定的数据
    /// </summary>
    protected void BindData()
    {

        DateTime start1 = Convert.ToDateTime(tbStartTime.Text);
        DateTime end1 = Convert.ToDateTime(tbEndTime.Text);
        int xAxisCount = Convert.ToInt32((end1 - start1).TotalDays) + 1;

        IList<TogoInfo> shops = new DeliverGroup().GetListWithOrderStatistics(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, orderWhere, ordersort);
        if (shops.Count > 0)
        {
            this.AspNetPager1.RecordCount = shops[0].DataID;
        }
        if (xAxisCount > 0)
        {
            foreach (var item in shops)
            {
                item.EBuilding = (item.ShopHaveMoney / xAxisCount).ToString("0.0");
                item.Tel = (item.allcount*1.0M / xAxisCount).ToString("0.0");

                if (item.fulltimedeliver > 0)
                {
                    item.fulltimeperorder = (item.fulltimeordercount / item.fulltimedeliver / xAxisCount).ToString("0.0");
                    item.fulltimeperfee = (item.fulltimeshopdiscountmoney / item.fulltimedeliver / xAxisCount).ToString("0.0");
                }
                if (item.parttimeodeliver > 0)
                {
                    item.parttimeperorder = (item.parttimeordercount / item.parttimeodeliver / xAxisCount).ToString("0.0");
                    item.parttimeperfee = (item.parttimeshopdiscountmoney / item.parttimeodeliver / xAxisCount).ToString("0.0");
                }


                item.fulltimeordercount =(item.fulltimeordercount  / xAxisCount);
                item.parttimeordercount = (item.parttimeordercount / xAxisCount);
                item.fulltimeshopdiscountmoney = (item.fulltimeshopdiscountmoney  / xAxisCount);
                item.parttimeshopdiscountmoney = (item.parttimeshopdiscountmoney / xAxisCount);

               

            }
        }

        //Response.Write(orderWhere);



        string sql = orderWhere;
        if (ddlcity.SelectedValue != "0")
        {
            sql += " and cityid = " + ddlcity.SelectedValue + "";
        }

        CustorderInfo count = new Custorder().GetSumSendTime(sql);
        lbadvance.InnerText = count.Advance.ToString();
        lbordercount.InnerText = count.OrderCount.ToString();
        lbordermoney.InnerText = count.OrderTotal.ToString();
        lballprice.InnerText = count.shopdiscountmoney.ToString();
        lbperorder.InnerText = (count.OrderCount * 1.0M / xAxisCount).ToString("0.0");
        lbpersendfee.InnerText = (count.shopdiscountmoney * 1.0M / xAxisCount).ToString("0.0");
        lbpersendtime.InnerText = Utils.second2MinAndSecond((count.delflag));
        lbperordertime.InnerText = Utils.second2MinAndSecond((count.UserId));
        timeoutcount.InnerText = count.deliversiteid.ToString();

        lbCommission.InnerText = count.Commission.ToString();

        timeoutrat.InnerText = "";
        if (count.OrderCount > 0)
        {
            timeoutrat.InnerText = (count.deliversiteid * 1.0 / count.OrderCount * 100).ToString("0.0") + "%";
        }

        this.rptsum.DataSource = shops;
        this.rptsum.DataBind();

        AlertScript.AjaxRegisterScript("init();", UpdatePanel1);
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }

    /// <summary>
    /// 时间变化搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void settime_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now; //当前时间
        string starttime = this.tbStartTime.Text;
        string endtime = this.tbEndTime.Text;
        Button bt = (Button)sender;
        switch (bt.ID)
        {
            case "btpre"://前一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "btyestoday"://昨天
                tbStartTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";

                break;
            case "tbtoday"://今天
                tbStartTime.Text = DateTime.Now.ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            case "btnext"://后一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "tbweek"://本周

                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));
                tbStartTime.Text = startWeek.ToDate();

                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbmonth"://本月
                tbStartTime.Text = DateTime.Now.ToString("yyyy-MM-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";
                break;
            case "tbyear"://本年
                tbStartTime.Text = DateTime.Now.ToString("yyyy-01-01") + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            default:
                break;
        }
        InitSqlwhere();

        BindData();
    }


    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = " 1 = 1 ";
        orderWhere = " 1=1 and Custorder.delflag = 0 ";
        orderWhere = new RightsTool().getOrderWhere(orderWhere);
        if (this.tbStartTime.Text != "")
        {
            orderWhere += " and  [OrderDateTime] >= '" + WebUtility.InputText(this.tbStartTime.Text) + "' ";
        }
        if (this.tbEndTime.Text != "")
        {
            orderWhere += " and OrderDateTime <= '" + WebUtility.InputText(this.tbEndTime.Text) + " 23:59:59'";
        }
        if (cbremoveexcel.Checked == true)
        {
            orderWhere += " and fromweb <> '" + (int)OrderSource.ExcelImport + "'";
        }

        string ids = WebUtility.GetcheckStr(rblstate);
        if (ids != "")
        {
            orderWhere += " and OrderStatus  in (" + ids + ")";
        }
      
        if (ddlcity.SelectedValue != "0")
        {
            SqlWhere += " and Status = " + ddlcity.SelectedValue + "";
        }

    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


}
